﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VPSoftTestApplication.Entity;
using VPSoftTestApplication.Presenters;

namespace VPSoftTestApplication.View
{
    public partial class RecordView : Form,IRecordView
    {
        public RecordView(Inventory inventory)
        {
            InitializeComponent();
            new RecordViewPresenter(inventory, this);
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                Save(null, null);
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        #region IRecordView Members

        public string Barcode
        {
            get
            {
                return barcodeTextBox.Text;
            }
            set
            {
                barcodeTextBox.Text = value;
            }
        }

        public string Product
        {
            get
            {
                return productTextBox.Text;
            }
            set
            {
                productTextBox.Text = value;
            }
        }

        public double Quantity
        {
            get
            {
                return Double.Parse(quantityTextBox.Text);
            }
            set
            {
                quantityTextBox.Text = value.ToString();
            }
        }

        public string InputType
        {
            get
            {
                return inputTypeTextBox.Text;
            }
            set
            {
                inputTypeTextBox.Text = value;
            }
        }

        public event EventHandler Save;

        #endregion

        private void quantityTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;

            if (!( Char.IsDigit(e.KeyChar) || e.KeyChar.ToString()==decSep || e.KeyChar == '\b'))
                e.KeyChar = Convert.ToChar(0);
        }
    }
}
